<?php

namespace App\Admin\Controllers;

use App\Models\SwtIdentity;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Carbon\Carbon;

class SwtIdentityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SwtIdentity::with('user'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.name','用户名');
            $grid->column('type') -> using(['卖方','买方']) -> label('info');
            $grid->column('realname');
            $grid->column('id_card');
            $grid->column('start_time') -> display( function ($value) {
                return $this->start_time .' 至 '. $this -> end_time;
            });
            $grid->column('status') -> using(['待审核','已通过','已驳回']) -> label(['info', 'success','danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->disableCreateButton();

            $grid -> actions(function (Grid\Displayers\Actions $actions) {
                $actions -> disableDelete();
                $actions -> disableEdit();

                if($actions -> row -> status != 1) {
                    $actions -> quickEdit();
                }
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtIdentity(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('card_front');
            $show->field('card_back');
            $show->field('realname');
            $show->field('id_card');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('alipay_name');
            $show->field('alipay_no');
            $show->field('alipay_code');
            $show->field('wechat_name');
            $show->field('wechat_no');
            $show->field('wechat_code');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(SwtIdentity::with('user'), function (Form $form) {
            $form->display('id');
            $form->display('user.name','用户名');
            $form->display('type') ->with(function ($value) {
                return $value ? '买方' : '卖方';
            });
            $form->display('card_front') ->with(function ($value) {
                return "<img src=".config('app.url').$value." />";
            });
            $form->display('card_back') ->with(function ($value) {
                return "<img src=".config('app.url').$value." />";
            });
            $form->display('alipay_name');
            $form->display('alipay_no');
            $form->display('wechat_name');
            $form->display('wechat_no');
            $form->radio('status') -> options([1=>'通过',2=>'驳回']);
        });
    }
}
